package org.aiforum.frountend.dao;


import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.aiforum.domain.Topic;
import org.springframework.stereotype.Repository;

/**
 * 发帖持久化实现类
 * @author VIC
 *
 */
@Repository("topicDao")
public class TopicDaoImpl implements TopicDao {
	
	/**
	 * 使用@PersistenceContext注解标注属性时,Spring会自动创建并注入实体管理器
	 */
	@PersistenceContext
	private EntityManager entityManager;

	@Override
	public void persist(Topic entity) {
		entityManager.persist(entity);
	}

	@Override
	public List<Topic> queryPageing(String hql, int firstIndex, int maxIndex) {
		Query query = entityManager.createQuery(hql);
		query.setFirstResult(firstIndex);
		query.setMaxResults(maxIndex);
		return query.getResultList();
	}

	@Override
	public Object queryLine(String hql) {
		Query query = entityManager.createQuery(hql);
		return query.getSingleResult();
	}

	@Override
	public Topic findById(int topicId) {
		return entityManager.find(Topic.class, topicId);
	}

}
